<?php

namespace aliyun;

use OSS\Core\OssException;
use OSS\OssClient;

/**
 * 阿里云Oss管理
 */
class AliyunOss
{

    //阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
    public string $accessKeyId;
    public string $accessKeySecret;
    //Endpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
    public string $endpoint;
    //填写Bucket名称，例如examplebucket。
    public string $bucket;
    //访问地址前缀
    public string $domainName;

    /**
     * 初始化
     */
    public function __construct()
    {
        //公用参数
        $this->accessKeyId = "LTAI5tJy8m4idXJtbMWzLGyb";
        $this->accessKeySecret = "wOVc2YRRG4yuAGQFiKgj0OROeN8nOr";
        //对象存储参数
        $this->endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
        $this->bucket = "dev-iot-v2";
        $this->domainName = "https://dev-iot-v2.oss-cn-shenzhen.aliyuncs.com/";
    }

    /**
     * 简单文件上传
     * @param string $local_path 本地文件地址
     * @param string $folder Object完整路径
     * @param string $suffix 文件后缀名
     * @return array
     */
    public function uploadFile(string $local_path, string $folder, string $suffix): array
    {
        try {
            //填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
            $object = $folder . '/' . date("Ymd_His", time()) . "_" . uniqid() . $suffix;
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $ossClient->uploadFile($this->bucket, $object, $local_path);
            return ['bool' => true, 'src' => $object, 'msg' => '上传成功'];
        } catch (OssException $e) {
            return ['bool' => false, 'src' => '', 'msg' => $e->getMessage()];
        }
    }

    /**
     * 获取文件地址
     * @param string $key
     * @return string
     */
    public function getObjectUrl(string $key): string
    {
        return $this->domainName . $key;
    }

    /**
     * 列举指定条件的文件
     * @param string $prefix 填写前缀，例如dir/
     * @param int $maxkeys 填写列举文件的最大个数
     * @return array
     */
    public function listObjects(string $prefix, int $maxkeys = 10): array
    {
        //配置文件
        $options = array(
            'delimiter' => '/',//填写对文件分组的字符，例如/
            'prefix' => $prefix,//填写前缀，例如dir/
            'max-keys' => $maxkeys,//填写列举文件的最大个数
            'marker' => '',//本次列举文件的起点。
        );
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $listObjectInfo = $ossClient->listObjects($this->bucket, $options);
        } catch (OssException $e) {
            return ['bool' => false, 'data' => [], 'msg' => $e->getMessage()];
        }
        //文件列表
        $objectList = $listObjectInfo->getObjectList();
        //目录列表。当匹配prefix的目录下无子目录或者设置delimiter为空时，目录列表不显示。
        $prefixList = $listObjectInfo->getPrefixList();
        //定义空数组
        $data = ['objectList' => [], 'prefixList' => []];
        if (!empty($objectList)) {
            foreach ($objectList as $objectInfo) {
                $data['objectList'][] = $objectInfo->getKey();
            }
        }
        if (!empty($prefixList)) {
            foreach ($prefixList as $prefixInfo) {
                $data['prefixList'][] = $prefixInfo->getPrefix();
            }
        }
        return ['bool' => true, 'data' => $data, 'msg' => '获取成功'];
    }

    /**
     * 删除单个文件
     * @param string $key
     * @return array
     */
    public function deleteObject(string $key): array
    {
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $ossClient->deleteObject($this->bucket, $key);
            return ['bool' => true, 'key' => $key, 'msg' => '删除成功'];
        } catch (OssException $e) {
            return ['bool' => false, 'key' => $key, 'msg' => $e->getMessage()];
        }
    }

}